from typing import Optional, List
from peewee import Model, CharField, IntegerField, AutoField, BooleanField, fn
from playhouse.shortcuts import model_to_dict
from pydantic import BaseModel

from db.my_sqlite import DB

class FAQ(Model):
    id = AutoField(primary_key=True, help_text="问答id")
    faq_name = CharField(help_text="问答")
    description = CharField(help_text="问答说明")
    userid = CharField(help_text="用户id")
    file_id = IntegerField(help_text="图像id")
    prologue = CharField(help_text="开场白")
    show_source = BooleanField(help_text="显示来源", null=True)
    show_quote = BooleanField(help_text="显示引用", null=True)
    doc_enhance = BooleanField(help_text="文档增强", null=True)
    rags = CharField(help_text="知识库")
    prompt = CharField(help_text="提示词")
    llm = CharField(help_text="大模型")
    create_date = CharField(help_text="创建时间")
    stop = BooleanField(help_text="是否停用", default=False)

    class Meta:
        database = DB

class FAQModel(BaseModel):
    id: Optional[int]= None
    faq_name:Optional[str]= None
    description: Optional[str]= None
    userid: Optional[int]= None
    file_id: Optional[int] = None
    prologue: Optional[str] = None
    show_source: Optional[bool] = None
    show_quote: Optional[bool] = None
    doc_enhance: Optional[bool] = None
    rags: Optional[str] = None
    prompt: Optional[str] = None
    llm: Optional[str] = None
    create_date: Optional[str] = None
    stop: Optional[bool] = None
    relative_file_path: Optional[str] = None

class FAQTable:
    def __init__(self, db):
        self.db = db
        self.db.create_tables([FAQ])

    @staticmethod
    def create_questions_and_answers(rags: str, faq_name: str, description: str, stop: int,create_date:str,userid:int) -> Optional[FAQModel]:
        file = FAQ.create(
            rags=rags,
            faq_name=faq_name,
            description=description,
            stop=stop,
            create_date=create_date,
            userid = userid
        )
        return FAQModel(**model_to_dict(file))
    @staticmethod
    def get_faqs_list(query: str,uid:str) -> List[
        FAQModel]:
        faq_query = FAQ.select()
        # if rag_id:
            # faq_query = faq_query.where(FAQ.rags == rag_id)
            # faq_query = faq_query.where(FAQ.rags ** f"%{rag_id}%")
        if query:
            faq_query = faq_query.where(FAQ.faq_name ** f"%{query}%")
        if uid:
            faq_query = faq_query.where(FAQ.userid == uid)
        result = []
        for faq in faq_query:
            file_statistics = FAQModel(
                id=faq.id,
                rags = faq.rags,
                userid=faq.userid,
                faq_name=faq.faq_name,
                description=faq.description,
                stop=faq.stop,
                create_date = faq.create_date
            )
            result.append(file_statistics)
        return result

    @staticmethod
    def update_faq(faq_name: str, description: str,faq_id:int,userid:int,file_id:int,prologue:str,rags:str,prompt:str,llm:str,show_source:bool = False,show_quote:bool = False,doc_enhance:bool = False) -> bool:
        return FAQ.update(
            faq_name=faq_name,
            description=description,
            userid=userid,
            file_id=file_id,
            prologue=prologue,
            show_source=show_source,
            show_quote=show_quote,
            doc_enhance=doc_enhance,
            prompt = prompt,
            llm = llm,
            rags=rags,
        ).where(
            (FAQ.id == faq_id)
        ).execute() > 0
    @staticmethod
    def delete_faq_by_id(faq_id: int) -> bool:
        return FAQ.delete().where(
            (FAQ.id == faq_id)
        ).execute() > 0

    @staticmethod
    def get_faq_detail_api(faq_id: str) -> Optional[FAQModel]:
        file = FAQ.get_or_none(
            (FAQ.id == faq_id)
        )
        if file:
            return FAQModel(**model_to_dict(file))
        return None

FAQ_DB = FAQTable(DB)